package com.infore.airsupervise.ui.fragment;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.beardedhen.androidbootstrap.BootstrapButton;
import com.beardedhen.androidbootstrap.BootstrapText;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.LawMenuE;
import com.infore.airsupervise.bean.MenuE;
import com.infore.airsupervise.other.listener.MainFragBtnClickListener;
import com.infore.airsupervise.ui.widget.CustomBadgeView;
import com.infore.airsupervise.utils.JumpUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 执法 Fragment
 */
public class LawFragment extends BaseFragment  {
    private static final String STR_GRID = "九宫格展示";
    private static final String STR_LINEAR = "列表展示";

    @BindView(R.id.titlebar_left_menu)
    AwesomeTextView titlebarLeftMenu;
    @BindView(R.id.titlebar_right_menu)
    AwesomeTextView titlebarRightMenu;
    @BindView(R.id.titlebar_title)
    TextView titlebarTitle;
    @BindView(R.id.rv_law_menu)
    RecyclerView rvLawMenu;
    @BindView(R.id.btn_layout_show)
    BootstrapButton btnLayoutShow;

    private  boolean isGridLayout =true;

    private int[] images = new int[]{R.drawable.img_01, R.drawable.img_02, R.drawable.img_03};

    private MainFragBtnClickListener listener;

    private BaseSectionQuickAdapter<LawMenuE, BaseViewHolder> adapter , linearAdapter;
    private List<LawMenuE> law_list = LawMenuE.getLawMenuEList();//列表集合

    public LawFragment() {
        // Required empty public constructor
    }

    @SuppressLint("ValidFragment")
    public LawFragment(MainFragBtnClickListener listener) {
        this.listener = listener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_law, container, false);
        unbinder = ButterKnife.bind(this, view);
        //顶部导航栏
        titlebarLeftMenu.setFontAwesomeIcon(FontAwesome.FA_USER);
        titlebarTitle.setText("移动执法");
        titlebarRightMenu.setVisibility(View.INVISIBLE);
        initGridList();
        rvLawMenu.addOnItemTouchListener(itemListener);
        return view;
    }
//    /**
//     * 图片轮播
//     */
//    private void initImageSlider(){
//        slider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom); //索引位置
//        slider.setDuration(3500);
//        DefaultSliderView sliderView;
//        for(int i = 0;i<3;i++){
//            sliderView = new DefaultSliderView(getContext());
//            sliderView.image(images[i]);
//            slider.addSlider(sliderView);
//        }
//    }
//    /**
//     * 初始化移动执法菜单列表
//     */
//    private void initRecycleView() {
//        rvLawMenu.setLayoutManager( new GridLayoutManager(getContext(),3));
//       adapter = new BaseQuickAdapter<MenuE, BaseViewHolder>(R.layout.item_chart_grid_view,law_list) {
//           @Override
//           protected void convert(BaseViewHolder helper, MenuE item) {
//               helper.setText(R.id.menu_title,item.getTitle())
//                       .setImageResource(R.id.menu_icon,item.getIcon());
//
//               CustomBadgeView badgeView = helper.getView(R.id.law_badge_view);
//               badgeView.setBadgeValue(item.getBadgeCount());
//           }
//       };
//       }

    /**
     * 网格展示列表
     */
    private void initGridList() {

        if(null == adapter ) {
            adapter = new BaseSectionQuickAdapter<LawMenuE, BaseViewHolder>(
                    R.layout.item_chart_grid_view, R.layout.item_pop_menu, law_list) {
                @Override
                protected void convertHead(BaseViewHolder helper, LawMenuE item) {
                    AwesomeTextView header = helper.getView(R.id.tv_menu);
                    BootstrapText.Builder builder = new BootstrapText.Builder(getContext());
                    builder.addText(item.header);
                    header.setBootstrapText(builder.build());
                    header.setBackgroundColor(ContextCompat.getColor(getContext(),android.R.color.white));
                    header.setPadding(16,16,16,16);
                }

                @Override
                protected void convert(BaseViewHolder helper, LawMenuE item) {
                    MenuE menuE = item.t;
                    helper.setText(R.id.menu_title, menuE.getTitle())
                            .setImageResource(R.id.menu_icon, menuE.getIcon());

                    CustomBadgeView badgeView = helper.getView(R.id.law_badge_view);
                    badgeView.setBadgeValue(menuE.getBadgeCount());
                }
            };
            adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
            adapter.isFirstOnly(false);

        }
        rvLawMenu.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
        rvLawMenu.setAdapter(adapter);
    }

    private void initLinearList() {
        if(null == linearAdapter) {
            linearAdapter = new BaseSectionQuickAdapter<LawMenuE, BaseViewHolder>(
                    R.layout.item_main_menu_rv, R.layout.item_pop_menu, law_list) {
                @Override
                protected void convertHead(BaseViewHolder helper, LawMenuE item) {
                    AwesomeTextView header = helper.getView(R.id.tv_menu);
                    BootstrapText.Builder builder = new BootstrapText.Builder(getContext());
                    builder.addText(item.header);
                    header.setBootstrapText(builder.build());

                }

                @Override
                protected void convert(BaseViewHolder helper, LawMenuE item) {
                    MenuE menuE = item.t;
                    helper.setImageResource(R.id.menu_icon, menuE.getIcon())
                            .setText(R.id.menu_title, menuE.getTitle())
                            .setText(R.id.menu_desc, menuE.getDesc());
                }
            };
            linearAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            linearAdapter.isFirstOnly(false);
        }
        rvLawMenu.setLayoutManager(new LinearLayoutManager(getContext()));
        rvLawMenu.setAdapter(linearAdapter);
    }

    /**
     * 展示抽屉布局
     */
    @OnClick(R.id.titlebar_left_menu)
    void leftMenuClick() {
        if (listener != null) {
            listener.onShowDrawerBtnClick();
        }
    }

    @OnClick(R.id.btn_layout_show)
    public void onClick() {
       String str_btn = "";
        if(!isGridLayout){
           initGridList();
            str_btn = STR_LINEAR;
        }else {
           initLinearList();
            str_btn = STR_GRID;
        }
        BootstrapText.Builder builder = new BootstrapText.Builder(getContext());
        builder.addText(str_btn);
        btnLayoutShow.setBootstrapText(builder.build());
        isGridLayout = !isGridLayout;
    }

    private RecyclerView.OnItemTouchListener itemListener = new OnItemClickListener() {
        @Override
        public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
            LawMenuE lawMenuE = law_list.get(position);
            if(!lawMenuE.isHeader){
                // 徽标置0
                lawMenuE.t.setBadgeCount(0);
                JumpUtil.jumpFromTo(
                        getContext(), lawMenuE.t.getFirstActivity(), Constants.INTENT_OBJ_KEY, lawMenuE.t);
                adapter.notifyItemChanged(position);
            }

        }
    };
}

